package com.googlecode.dgwt.client.dojox.data.model;

import java.util.Map;

import com.googlecode.dgwt.client.dojo.data.format.ValueFormatter;
import com.googlecode.dgwt.client.dojox.data.model.base.DataColumnBase;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class MapColumn extends DataColumnBase<Map<String,Object>, Object> {
        
    private Class<? extends Object> columnType;

    public MapColumn(Class<? extends Object> columnType, String name) {
        super();
        setColumnType(columnType);
        setName(name);
    }

    public MapColumn(Class<? extends Object> columnType, String name, String title) {
        super(title);
        setColumnType(columnType);
        setName(name);
    }
    
    public MapColumn(Class<? extends Object> columnType, String name, String title, int maxLength) {
        super(title, maxLength);
        setColumnType(columnType);
        setName(name);
    }

    public MapColumn(Class<? extends Object> columnType, String name, String title, int maxLength, ValueFormatter<Object> formatter) {
        super(title, maxLength, formatter);
        setColumnType(columnType);
        setName(name);
    }

    @Override
    public Object onGetValue(Map<String, Object> item) {
        return item.get(getName());
    }

    @Override
    public void onSetValue(Map<String, Object> item, Object value) {
        item.put(getName(), value);
    }

    @Override
    public Class<? extends Object> getColumnType() {
        return columnType;
    }

    public void setColumnType(Class<? extends Object> classType) {
        if (classType == null){
            classType = Object.class;
        }
        this.columnType = classType;
        updateFormatter();
    }

}
